package me.zhanshi123.vipsystem.command;

import cn.nukkit.command.Command;
import cn.nukkit.command.CommandSender;
import me.zhanshi123.vipsystem.Main;
import me.zhanshi123.vipsystem.command.type.AdminCommand;
import me.zhanshi123.vipsystem.command.type.PermissionCommand;
import me.zhanshi123.vipsystem.manager.MessageManager;

import java.text.MessageFormat;

public class Commands extends Command {

    public Commands() {
        super("vipsys", "Manage your timed vip");
        this.setAliases(new String[]{"vip"});
    }

    private boolean handle(CommandSender commandSender, String label, String[] args, SubCommand subCommand) {
        if (subCommand.isNeedArg()) {
            int argLength = subCommand.getArgLength();
            if (argLength != args.length) {
                subCommand.sendHelp(commandSender);
                return true;
            } else {
                return subCommand.onCommand(commandSender, label, args);
            }
        } else {
            return subCommand.onCommand(commandSender, label, args);
        }
    }

    private void sendHelp(CommandSender sender) {
        Main.getCommandHandler().getCommands().stream()
                .filter(subCommand -> !(subCommand instanceof AdminCommand))
                .filter(subCommand -> subCommand.getDescription() != null)
                .forEach(subCommand -> sender.sendMessage(MessageFormat.format(MessageManager.getString("Command.format"), subCommand.getUsage(), subCommand.getDescription())));
        if (sender.isOp()) {
            Main.getCommandHandler().getCommands().stream()
                    .filter(subCommand -> subCommand instanceof AdminCommand)
                    .filter(subCommand -> subCommand.getDescription() != null)
                    .forEach(subCommand -> sender.sendMessage(MessageFormat.format(MessageManager.getString("Command.format"), subCommand.getUsage(), subCommand.getDescription())));
        }
    }

    @Override
    public boolean execute(CommandSender commandSender, String label, String[] args) {
        if (args.length == 0) {
            sendHelp(commandSender);
            return true;
        } else {
            String arg = args[0];
            SubCommand subCommand = Main.getCommandHandler().getSubCommand(arg);
            if (subCommand == null) {
                subCommand = Main.getCommandHandler().getSubCommand("*");
            }
            if (subCommand == null) {
                return true;
            }
            if (subCommand instanceof AdminCommand) {
                if (commandSender.isOp()) {
                    return handle(commandSender, label, args, subCommand);
                } else {
                    commandSender.sendMessage(MessageManager.getString("permissionDeny"));
                    return true;
                }
            } else if (subCommand instanceof PermissionCommand) {
                if (commandSender.hasPermission("me.zhanshi123.vipsystem." + subCommand.getName())) {
                    return handle(commandSender, label, args, subCommand);
                } else {
                    commandSender.sendMessage(MessageManager.getString("permissionDeny"));
                    return true;
                }
            } else {
                return handle(commandSender, label, args, subCommand);
            }
        }
    }
}
